package org.go.scheduler.jmx;

import static javax.management.openmbean.SimpleType.BOOLEAN;
import static javax.management.openmbean.SimpleType.STRING;

import java.util.ArrayList;
import java.util.Map;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import org.go.Go;
import org.go.work.Work;

public class JobDetailSupport {
	private static final CompositeType COMPOSITE_TYPE;
	private static final String COMPOSITE_TYPE_DESCRIPTION = "Job Execution Details";
	private static final String COMPOSITE_TYPE_NAME = "JobDetail";
	private static final String[] INDEX_NAMES = new String[] { "name", "group" };
	private static final String[] ITEM_DESCRIPTIONS = new String[] { "name", "group", "description", "jobClass", "jobDataMap", "durability", "shouldRecover", };
	private static final String[] ITEM_NAMES = new String[] { "name", "group", "description", "jobClass", "jobDataMap", "durability", "shouldRecover", };
	private static final OpenType[] ITEM_TYPES = new OpenType[] { STRING, STRING, STRING, STRING, WorkDataMapSupport.TABULAR_TYPE, BOOLEAN, BOOLEAN, };
	private static final TabularType TABULAR_TYPE;
	private static final String TABULAR_TYPE_DESCRIPTION = "JobDetail collection";
	private static final String TABULAR_TYPE_NAME = "JobDetail collection";

	static {
		try {
			COMPOSITE_TYPE = new CompositeType(COMPOSITE_TYPE_NAME, COMPOSITE_TYPE_DESCRIPTION, ITEM_NAMES, ITEM_DESCRIPTIONS, ITEM_TYPES);
			TABULAR_TYPE = new TabularType(TABULAR_TYPE_NAME, TABULAR_TYPE_DESCRIPTION, COMPOSITE_TYPE, INDEX_NAMES);
		} catch (OpenDataException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @param cData
	 * @return JobDetail
	 */
	public static Work newJobDetail(CompositeData cData) throws ClassNotFoundException {
		int i = 0;
		Class<Go> jobClass = (Class<Go>) Class.forName((String) cData.get(ITEM_NAMES[i++]));
		Work jobDetail = null;
		/*
		new Work(jobClass.newInstance());
		jobDetail.setName((String) cData.get(ITEM_NAMES[i++]));
		jobDetail.setGroup((String) cData.get(ITEM_NAMES[i++]));
		jobDetail.setDescription((String) cData.get(ITEM_NAMES[i++]));
		jobDetail.setJobClass((Class<? extends Go>) jobClass);
		jobDetail.setJobDataMap(WorkDataMapSupport.newJobDataMap((TabularData) cData.get(ITEM_NAMES[i++])));
		jobDetail.setDurability((Boolean) cData.get(ITEM_NAMES[i++]));
		jobDetail.setRequestsRecovery((Boolean) cData.get(ITEM_NAMES[i++]));
		*/
		return jobDetail;
	}

	/**
	 * @param Map<String, Object>
	 * @return JobDetail
	 */
	public static Work newJobDetail(Map<String, Object> attrMap) throws ClassNotFoundException {
		int i = 0;
		Class<Go> jobClass = (Class<Go>) Class.forName((String) attrMap.get(ITEM_NAMES[i++]));
		Work jobDetail = null;
		//new Work(jobClass.newInstance());

		jobDetail.setWorkId((String) attrMap.get(ITEM_NAMES[i++]));
		//jobDetail.setGroup((String) attrMap.get(ITEM_NAMES[i++]));
		jobDetail.setDescription((String) attrMap.get(ITEM_NAMES[i++]));
		//jobDetail.setJobClass((Class<? extends Go>) jobClass);
		if (attrMap.containsKey(ITEM_NAMES[i])) {
			//jobDetail.setJobDataMap(WorkDataMapSupport.newJobDataMap((Map) attrMap.get(ITEM_NAMES[i])));
		}
		i++;
		if (attrMap.containsKey(ITEM_NAMES[i])) {
			//jobDetail.setDurability((Boolean) attrMap.get(ITEM_NAMES[i]));
		}
		i++;
		if (attrMap.containsKey(ITEM_NAMES[i])) {
			//jobDetail.setRequestsRecovery((Boolean) attrMap.get(ITEM_NAMES[i]));
		}
		i++;

		return jobDetail;
	}

	/**
	 * @param jobDetail
	 * @return CompositeData
	 */
	public static CompositeData toCompositeData(Work jobDetail) {
		//try {
		return null;
		//new CompositeDataSupport(COMPOSITE_TYPE, ITEM_NAMES, new Object[] { jobDetail.getKey().getName(), jobDetail.getKey().getGroup(), jobDetail.getDescription(), jobDetail.getWorkClass().getName(),
		//WorkDataMapSupport.toTabularData(jobDetail.getJobDataMap()), jobDetail.isDurable(), jobDetail.requestsRecovery(), });
		//} catch (OpenDataException e) {
		//throw new RuntimeException(e);
		//}
	}

	public static TabularData toTabularData(Work[] jobDetails) {
		TabularData tData = new TabularDataSupport(TABULAR_TYPE);
		if (jobDetails != null) {
			ArrayList<CompositeData> list = new ArrayList<CompositeData>();
			for (Work jobDetail : jobDetails) {
				list.add(toCompositeData(jobDetail));
			}
			tData.putAll(list.toArray(new CompositeData[list.size()]));
		}
		return tData;
	}

}
